<?php
class CmsPostModel extends Model
{
    protected $resourceId = 'AdminResource';
    protected static $instance;

    public static function GetInstance()
    {
        return self::$instance ? self::$instance : self::$instance = new CmsPostModel();
    }

    protected function LoadFields()
    {
        parent::LoadFields();
        $this->SetForeignKey('createdby', 'cms_user', 'id');
        $this->SetForeignKey('modifiedby', 'cms_user', 'id');

        $this->AddOneToManyRelation('cms_post_tag', 'post_id');
        $this->AddOneToManyRelation('cms_comment', 'post_id');
    }

    public function GetByTagId($tagId)
    {
        if ($tagId)
            return CmsPostTagModel::GetInstance()->FindBy('tag_id', $tagId, 'post_id AS id');
        else
            return $this->Find(null, 'id', 'datecreated DESC', 20);
    }
}
?>
